#include <QTime>
#include <QDebug>
#include <QObject>
#include <QTcpSocket>
#include <qtcpserver.h>
#include <qtcpsocket.h>
#include <QByteArray>
#include <QThread>
#include <time.h>

#include "WorkThreadManage/BackTcpThreadManage.h"
#include "Interface/PluginInterface/tecplugininterface.h"
#include "uniontecsystemdll_global.h"

#include "CommunicationMange/SimpleTcpClient/simpletcpsocketdll_global.h"
#include "CommunicationMange/SimpleTcpServer/simpletcpserverdll_global.h"
#include "CommunicationMange/KukaRsiUdpServerDLL/kukarsiudpserverdll_global.h"

BackTcpThread::BackTcpThread(){
    ThreadStatus = ENUM_ThreadStatus::ThreadInit;
    PluginStatus = ENUM_PluginStatus::Ready;
    KUKAPluginStatus = ENUM_PluginStatus::Ready;

    ptecPluginInstance = nullptr;
    pkukatecPluginInstance = nullptr;

    mThreadLoopTime.LoopTime = 0;
    mThreadLoopTime.TimeLast = 0;
    mThreadLoopTime.TimeNow = 0;
}
BackTcpThread::~BackTcpThread(){
    ThreadStatus = ENUM_ThreadStatus::ThreadStop;

    QThread::quit();
    QThread::wait(10);
}

short BackTcpThread::PluginInstanceConnect(tecPluginInterface* p){
    ptecPluginInstance = p;
    return NOERROR;
}

short  BackTcpThread::KUKAPluginInstanceConnect(kukatecPluginInterface* p){
    pkukatecPluginInstance = p;
    return NOERROR;
}

double BackTcpThread::GetThreadLoopTime(){
    return mThreadLoopTime.LoopTime;
}

void BackTcpThread::run(){
    COM_ComInstanceInit();
    COM_ComServerInstanceInit();
    ThreadStatus = ENUM_ThreadStatus::ThreadRunning;
    for(;;){
        if(ThreadStatus == ENUM_ThreadStatus::ThreadRunning){
            mThreadLoopTime.TimeNow = QDateTime::currentDateTime().toMSecsSinceEpoch();
            mThreadLoopTime.LoopTime = mThreadLoopTime.TimeNow - mThreadLoopTime.TimeLast;
            if(mThreadLoopTime.LoopTime == 0) mThreadLoopTime.LoopTime = -1;
            mThreadLoopTime.TimeLast = mThreadLoopTime.TimeNow;

            if(COM_InstanceExist()){
                COM_clientMainThreadLoopRun(LOOPRUN_STATECHANGE);
                COM_clientMainThreadLoopRun(LOOPRUN_RECEIVE);
            }
            if(COM_ComServerInstanceExist()){
                COM_serverMainThreadLoopRun(LOOPRUN_STATECHANGE);
                COM_serverMainThreadLoopRun(LOOPRUN_RECEIVE);
            }

            if(KUKAPluginStatus == ENUM_PluginStatus::Ready){  }
            else{
                if(KUKAPluginStatus == ENUM_PluginStatus::Init){
                    if(pkukatecPluginInstance != NULL){
                        this->pkukatecPluginInstance->RegisterMainInitTask();
                        KUKAPluginStatus = ENUM_PluginStatus::InitDone;

                    }
                }
                else if(KUKAPluginStatus == ENUM_PluginStatus::InitDone){  }
                else if(KUKAPluginStatus == ENUM_PluginStatus::Running){
                    if(pkukatecPluginInstance != NULL){
                        this->pkukatecPluginInstance->RegisterMainLoopTask();
                    }
                }
                else if(KUKAPluginStatus == ENUM_PluginStatus::Pause){  }
                else if(KUKAPluginStatus == ENUM_PluginStatus::Delete){
                    if(pkukatecPluginInstance != NULL){
                        this->pkukatecPluginInstance = nullptr;
                        KUKAPluginStatus = ENUM_PluginStatus::Ready;
                    }
                }
            }

            if(PluginStatus == ENUM_PluginStatus::Ready){  }
            else{
                if(PluginStatus == ENUM_PluginStatus::Init){
                    if(ptecPluginInstance != NULL){
                        this->ptecPluginInstance->RegisterMainInitTask();
                        PluginStatus = ENUM_PluginStatus::InitDone;
                    }
                }
                else if(PluginStatus == ENUM_PluginStatus::InitDone){  }
                else if(PluginStatus == ENUM_PluginStatus::Running){
                    if(ptecPluginInstance != NULL){
                        this->ptecPluginInstance->RegisterMainLoopTask();
                    }
                }
                else if(PluginStatus == ENUM_PluginStatus::Pause){  }
                else if(PluginStatus == ENUM_PluginStatus::Delete){
                    if(ptecPluginInstance != NULL){
                        this->ptecPluginInstance = nullptr;
                        PluginStatus = ENUM_PluginStatus::Ready;
                    }
                }
            }

            if(COM_InstanceExist()){
                COM_clientMainThreadLoopRun(LOOPRUN_SEND);
            }
            if(COM_ComServerInstanceExist()){
                COM_serverMainThreadLoopRun(LOOPRUN_SEND);
            }
        }

        else if(ThreadStatus == ENUM_ThreadStatus::ThreadStop){
            if(ptecPluginInstance != nullptr){
                ptecPluginInstance = nullptr;
            }
            if(pkukatecPluginInstance != nullptr){
                pkukatecPluginInstance = nullptr;
            }

            if(COM_InstanceExist()){
                COM_ComInstanceRelease();
            }
            if(COM_ComServerInstanceExist()){
                COM_ComServerInstanceRelease();
            }
            break;
        }
    }
}

short  BackTcpThread::PluginStatusTransitions(ENUM_PluginStatus Status){
    if(this->pkukatecPluginInstance != nullptr){
        this->KUKAPluginStatus = Status;
    }
    if(this->ptecPluginInstance != nullptr){
        this->PluginStatus = Status;
    }
    return NOERROR;
}
